package gui.standard.form;

import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.tablemodel.SektorTableModel;
import net.miginfocom.swing.MigLayout;
import util.components.JLimitedTextField;
import actions.standard.form.ZoomFormAction;

public class SektorForm extends StandardForm {

	private static final long serialVersionUID = 1L;
	private JLimitedTextField tfSifraPreduzeca;
	private JLimitedTextField tfSifraSektora;
	private JLimitedTextField tfNazivSektora;

	private JButton bPreduzeceZoom;

	public SektorForm() {

	}
	
	@Override
	public void init(){
		
		super.init();
		
		tfSifraPreduzeca = new JLimitedTextField(12, 12, true);
		tfSifraSektora = new JLimitedTextField(12, 12, true);
		tfNazivSektora = new JLimitedTextField(30, 30, true);
		
		setTitle("Sektor");
		
		tableModel = new SektorTableModel(new String[] {
				"Sifra preduzeca", "Sifra sektora", "Naziv sektora" }, 0);
		
		tblGrid.setModel(tableModel);
		
		bPreduzeceZoom = new JButton(new ZoomFormAction(this,
					new PreduzeceForm()));
		
		children.add(new MagacinForm());

		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new MigLayout("fillx"));
		JPanel dataPanel = new JPanel();
		dataPanel.setLayout(new MigLayout("gapx 15px"));

		JLabel lblSifraPreduzeca = new JLabel("Sifra preduzeca:");
		JLabel lblSifraSektora = new JLabel("Sifra sektora:");
		JLabel lblNazivSektora = new JLabel("Naziv sektora:");

		dataPanel.add(lblSifraPreduzeca);
		dataPanel.add(tfSifraPreduzeca);
		dataPanel.add(bPreduzeceZoom, "wrap");
		dataPanel.add(lblSifraSektora);
		dataPanel.add(tfSifraSektora, "pushx");
		dataPanel.add(lblNazivSektora);
		dataPanel.add(tfNazivSektora, "wrap");
		bottomPanel.add(dataPanel);

		bottomPanel.add(buttonsPanel, "dock east");

		add(bottomPanel, "grow, wrap");
	}

	@Override
	public void sync() {
		// TODO Auto-generated method stub
		int index = tblGrid.getSelectedRow();
		if (index < 0) {
			tfSifraPreduzeca.setText("");
			tfSifraSektora.setText("");
			tfNazivSektora.setText("");
			return;
		}
		String sifraPreduzeca = (String) tableModel.getValueAt(index, 0);
		String sifraSektora = (String) tableModel.getValueAt(index, 1);
		String nazivSektora = (String) tableModel.getValueAt(index, 2);

		tfSifraPreduzeca.setText(sifraPreduzeca);
		tfSifraSektora.setText(sifraSektora);
		tfNazivSektora.setText(nazivSektora);

		setMode(StandardForm.MODE_EDIT);
	}

	@Override
	public void addRow() {
		// TODO Auto-generated method stub
		String sifraPreduzeca = tfSifraPreduzeca.getText().trim();
		String sifraSektora = tfSifraSektora.getText().trim();
		String nazivSektora = tfNazivSektora.getText().trim();
		try {
			int index = tableModel.insertRow(new String[] { sifraPreduzeca,
					sifraSektora, nazivSektora });
			tblGrid.setRowSelectionInterval(index, index);
			setMode(MODE_ADD);
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void searchRow() {
		// TODO Auto-generated method stub
		String sifraPreduzeca = tfSifraPreduzeca.getText().trim();
		String sifraSektora = tfSifraSektora.getText().trim();
		String nazivSektora = tfNazivSektora.getText().trim();
		try {
			tableModel.searchRows(new String[] { sifraPreduzeca, sifraSektora,
					nazivSektora });
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void editRow() {
		// TODO Auto-generated method stub
		String sifraPreduzeca = tfSifraPreduzeca.getText().trim();
		String sifraSektora = tfSifraSektora.getText().trim();
		String nazivSektora = tfNazivSektora.getText().trim();
		try {
			int index = tableModel.editRow(new String[] { sifraPreduzeca,
					sifraSektora, nazivSektora }, tblGrid.getSelectedRow());
			tblGrid.setRowSelectionInterval(index, index);
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void setMode(int mode) {
		super.setMode(mode);

		if (mode != StandardForm.MODE_ADD)
			return;

		tfSifraPreduzeca.setText("");
		tfSifraSektora.setText("");
		tfNazivSektora.setText("");
	}

	@Override
	public String[] getSelectedRowKeys() {
		// TODO Auto-generated method stub
		return new String[]{tfSifraPreduzeca.getText().trim(), tfSifraSektora.getText().trim()};
	}

	@Override
	public void getZoomedCol(String[] keys, StandardForm form) {
		// TODO Auto-generated method stub
		if(form instanceof PreduzeceForm)
			tfSifraPreduzeca.setText(keys[0]);
	}

	@Override
	public String[] getSelectedRowNextKeys(int index) {
		// TODO Auto-generated method stub
		return new String[]{tfSifraPreduzeca.getText().trim(), tfSifraSektora.getText().trim(), ""};
	}

}
